<%--
  - Copyright 2011 Angel Sanadinov
  -
  - This file is part of VBox WMI.
  -
  - VBox WMI is free software: you can redistribute it and/or modify
  - it under the terms of the GNU General Public License as published by
  - the Free Software Foundation, either version 3 of the License, or
  - (at your option) any later version.
  -
  - VBox WMI is distributed in the hope that it will be useful,
  - but WITHOUT ANY WARRANTY; without even the implied warranty of
  - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  - GNU General Public License for more details.
  -
  - You should have received a copy of the GNU General Public License
  - along with VBox WMI.  If not, see <http://www.gnu.org/licenses/>.
  --%>

<%@page import="Utilities.ExceptionMessages"%>
<%@page import="Utilities.ApplicationException"%>
<%@page import="Beans.Responses.ResponseBean"%>
<%@page import="Managers.Utilities.RequestResponseWrapper"%>
<%@page import="Managers.Utilities.ResponseMessagesContainer"%>
<%@page import="Utilities.Functions"%>
<%@page import="Utilities.UIFunctions"%>
<%@page import="Beans.Requests.ServerManagementRequest"%>
<%@page import="Utilities.Constants"%>
<%@page import="Beans.Responses.ServerManagementResponse"%>
<%@page import="Utilities.Constants.SendingRequestResult"%>
<%@page import="Utilities.ParameterNames"%>
<%@page import="Utilities.ParameterNames.Service"%>
<%@page import="Managers.Utilities.User"%>
<%
    //retrieves user data from the session
    User userSessionData = (User)session.getAttribute(ParameterNames.SESSION_USER_OBJECT);

    //attempts to retrieve the supplied action ID
    int action = 0;
    try
    {
        action = Integer.parseInt(request.getParameter("action"));
    }
    catch(NumberFormatException e){}

    //attempts to retrieve the supplied server ID
    int manageServerId = Constants.INVALID_SERVER_ID;
    try
    {
        manageServerId = Integer.parseInt(request.getParameter("id"));
    }
    catch(NumberFormatException e){}

    ServerManagementRequest serverRequest = null;
    switch(action)
    {
        //create server request
        case 1:
        {
            int portsRangeLow = 0, portsRangeHigh = 0, portsPerMachine = 0;
            try
            {
                portsRangeLow = Integer.parseInt(request.getParameter("create.portsLow"));
                portsRangeHigh = Integer.parseInt(request.getParameter("create.portsHigh"));
                portsPerMachine = Integer.parseInt(request.getParameter("create.portsPerMachine"));
            }
            catch(NumberFormatException e){}

            serverRequest = new ServerManagementRequest(userSessionData.getUserData().getUserId(),
                                                        request.getParameter("create.name"),
                                                        request.getParameter("create.description"),
                                                        request.getParameter("create.wsAddress"),
                                                        request.getParameter("create.wsUser"),
                                                        request.getParameter("create.wsPass"),
                                                        request.getParameter("create.sfRoot"),
                                                        portsRangeLow,
                                                        portsRangeHigh,
                                                        portsPerMachine);
        } break;

        //modify server request
        case 2:
        {
            int portsRangeLow = 0, portsRangeHigh = 0, portsPerMachine = 0;
            try
            {
                portsRangeLow = Integer.parseInt(request.getParameter("modify.portsLow"));
                portsRangeHigh = Integer.parseInt(request.getParameter("modify.portsHigh"));
                portsPerMachine = Integer.parseInt(request.getParameter("modify.portsPerMachine"));
            }
            catch(NumberFormatException e){}

            serverRequest = new ServerManagementRequest(userSessionData.getUserData().getUserId(),
                                                        manageServerId,
                                                        request.getParameter("modify.name"),
                                                        request.getParameter("modify.description"),
                                                        request.getParameter("modify.wsAddress"),
                                                        request.getParameter("modify.wsUser"),
                                                        request.getParameter("modify.wsPass"),
                                                        request.getParameter("modify.sfRoot"),
                                                        portsRangeLow,
                                                        portsRangeHigh,
                                                        portsPerMachine);
        } break;
        
        //remove server request
        case 3:
            serverRequest = new ServerManagementRequest(userSessionData.getUserData().getUserId(), manageServerId); break;

        default: ; break;
    }

    //attempts to send the request and retrieves the response
    SendingRequestResult sendingResult = UIFunctions.sendServiceRequest(request, response, serverRequest, Service.SERVERS_MANAGEMENT);
    if(sendingResult != SendingRequestResult.OK)
    {
        //retrieves the user's messages container
        ResponseMessagesContainer messagesContainer =
            (ResponseMessagesContainer)request.getSession(false).getAttribute(ParameterNames.SESSION_REQUEST_MESSAGES_OBJECT);
        
        int exceptionCode = UIFunctions.parseSendingRequestResult(sendingResult);

        //adds the response from the failed operation
        ServerManagementResponse serverResponse = new ServerManagementResponse(false, new ApplicationException(exceptionCode));
        messagesContainer.put(new RequestResponseWrapper(serverRequest, serverResponse));
    }
    else
        ;

    //based on the requested action, the proper redirect page is selected
    if(Functions.isServerIdValid(manageServerId) && action != 3)
        response.sendRedirect("servers.jsp?id=" + manageServerId);
    else
        response.sendRedirect("servers.jsp");
%>